<?php

namespace app\admin\model;


/**
 * 设计师模型
 * Class InformationClassification
 * @package app\admin\model
 */
class Designer extends Base
{
    protected $auto = ["substation_name","designer_level_name"];

    //auto，分站名称
    protected function setSubstationNameAttr($value,$data)
    {
        $Substation = new Substation();//分站类
        return $Substation->getFieldValue("substation_id", $data["substation_id"], "substation_name");
    }

    //auto，设计师级别名称
    protected function setDesignerLevelNameAttr($value,$data)
    {
        $Config = new Config();//分站类
        return $Config->getConfigName($data["designer_level"]);
    }

    //获取代表作品
    public function getRepresentativeWorksTextAttr($value,$data)
    {
        $representative_works = StrToArray($data["representative_works"]);
        $Drawing = new Drawing();
        if(!empty($representative_works)){
            foreach ($representative_works as $val) {
                $PkInfo = $Drawing->PkInfo($val);
                if(!empty($PkInfo)) {
                    $value .= " 《" . $PkInfo->no . " >> " . $PkInfo->no2_name . "》";
                }
            }
        }
        return $value;
    }

    //获取栏目列表
    public function designerList($keyword){
        return $this
            ->where(config("TableDeleteField"),0)//获取没有删除的
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->whereOr("config_name","like","%".$keyword."%");
                }
            })
            ->where(function ($q){
                if(!empty($this->substations)){//是否设置了当前网站
                    $q->where("substation_id",$this->substations);
                }else{
                    if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否超级管理员
                        $arr = [];
                        $Substation = new Substation();//分站类
                        $userList = $Substation->userList();//获取分站集合
                        foreach ($userList as $val){
                            $arr[] = $val['substation_id'];
                        }
                        $q->where("substation_id","in",$arr);
                    }
                }
            })
            ->order("substation_id ASC,sort DESC,is_display DESC")
            ->page($this->after_page)
            ->limit($this->after_limit)
            ->select();
    }

    //获取栏目列表总数
    public function designerListCount($keyword){
        return $this
            ->where(config("TableDeleteField"),0)//获取没有删除的
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->whereOr("config_name","like","%".$keyword."%");
                }
            })
            ->where(function ($q){
                if(!empty($this->substations)){//是否设置了当前网站
                    $q->where("substation_id",$this->substations);
                }else{
                    if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否超级管理员
                        $arr = [];
                        $Substation = new Substation();//分站类
                        $userList = $Substation->userList();//获取分站集合
                        foreach ($userList as $val){
                            $arr[] = $val['substation_id'];
                        }
                        $q->where("substation_id","in",$arr);
                    }
                }
            })
            ->count();
    }


    //验证是否有权限操作
    public function checkSave($designer_id,$substation_id){
        $is = [false,false];//验证标识
        if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否超级管理员
            $Substation = new Substation();//分站类
            $userList = $Substation->userList();//获取分站集合
            $classification_substation_id = $this->where("designer_id",$designer_id)->value("substation_id");//获取原分站ID
            foreach ($userList as $val){//遍历分站集合
                if(!empty($designer_id)) {//修改验证
                    if ($classification_substation_id == $val["substation_id"]) {
                        $is[0] = true;
                    }
                }else{
                    //添加不验证
                    $is[0] = true;
                }
                if(!empty($substation_id)) {//添加修改验证
                    if ($substation_id == $val["substation_id"]) {
                        $is[1] = true;
                    }
                }else{
                    //删除不验证
                    $is[1] = true;
                }
            }
        }else{
            $is = [true,true];
        }
        return $is[0] === true && $is[1] === true ? true : false;
    }
}